package com.mitake.parse;

import java.util.ArrayList;
import java.util.Collections;

import com.mitake.utils.ParseUtil;

public class MitakeQuote
{
	
	public static Stock parseDetail(String src)
	{
		Stock stock = new Stock();

		String[] block = src.split(ParseUtil.SPLIT_0x04);

		for(int i = 0; i < block.length; i++)
		{
			if(i == 0)
			{
				String[] column = block[0].split(ParseUtil.SPLIT_0x02);

				for(int j = 0; j < column.length; j++)
				{
					parseColumn(j, column[j], stock);
				}

				calculateColumnValue(stock);
				/**
				 * 格式化欄位
				 */
				formatColumnValue(stock);
			}
			else if(i == 1)
			{
				/**
				 * L.d("{买卖符号[2]时间[2]成交量[2]成交价[3]");
				 */
				String[] rows = block[1].split(ParseUtil.SPLIT_0x03);

				int count = rows.length;

				if(count > 0)
				{
					stock.tradeTick = new String[count][4];

					for(int j = 0; j < count; j++)
					{
						String[] item = rows[j].split(ParseUtil.SPLIT_0x02);
						/**
						 * 20150519[李欣駿] 只將有回傳的資料放入 买卖符号[2]时间[2]成交量[2]成交价[3]
						 */
						int len = item.length > 4 ? 4 : item.length;

						for(int k = 0; k < len; k++)
						{
							if(k == 3)
							{
								/**
								 * 价位要格式化
								 */
								item[k] = ParseUtil.base93.getDecodeNumber(item[k]);// 水晶:上證
								stock.tradeTick[j][k] = formatPrice(item[k], stock.market, stock.subtype);
							}
							else if(k == 1)
							{
								item[k] = ParseUtil.base93.getDecodeNumber(item[k]);
								item[k] = String.format("%08d", Long.parseLong(item[k]));

								stock.tradeTick[j][k] = item[k];
							}
							else
							{
								item[k] = ParseUtil.base93.getDecodeNumber(item[k]);
								stock.tradeTick[j][k] = item[k];
							}

						}
					}
					
					StringBuilder data = new StringBuilder();
					
					for(int j = 0 ; j < stock.tradeTick.length ; j++)
					{
						if(j > 0)
						{
							data.append(",");
						}
						
						data.append("[");
						
						for(int k = 0 ; k < stock.tradeTick[j].length ; k++)
						{
							if(k > 0)
							{
								data.append(",");
							}
							
							data.append(stock.tradeTick[j][k]);
						}
						
						data.append("]");
					}
					
					stock.tradeTickTmp = data.toString();
				}
			}
			else if(i == 2)
			{
				/**
				 * {上漲家數}[2]{平盤家數}[2]{下跌家數}[2]
				 */
				if(block[2].trim().length() > 0)
				{
					String[] data = block[2].split(ParseUtil.SPLIT_0x02);

					for(int j = 0; j < data.length; j++)
					{
						if(j == 0)
						{
							stock.upCount = data[j];
						}
						else if(j == 1)
						{
							stock.sameCount = data[j];
						}
						else if(j == 2)
						{
							stock.downCount = data[j];
						}
					}
				}
			}
			else if(i == 3)
			{
				if(block[3].trim().length() > 0)
				{
					String[] data = block[3].split(ParseUtil.SPLIT_0x02);

					for(int j = 0; j < data.length; j++)
					{
						if(j == 0)
						{
							stock.contractID = data[j];
						}
						else if(j == 1)
						{
							stock.objectID = data[j];
						}
						else if(j == 2)
						{
							stock.stockSymble = data[j];
						}
						else if(j == 3)
						{
							stock.stockType = data[j];
						}
						else if(j == 4)
						{
							stock.stockUnit = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 5)
						{
							stock.exePrice = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 6)
						{
							stock.startDate = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 7)
						{
							stock.endDate = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 8)
						{
							stock.exeDate = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 9)
						{
							stock.delDate = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 10)
						{
							stock.expDate = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 11)
						{
							stock.version = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 12)
						{
							stock.presetPrice = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 13)
						{
							stock.setPrice = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 14)
						{
							stock.stockClose = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 15)
						{
							stock.stockLast = ParseUtil.base93.getDecodeNumber(data[j]);
						}
						else if(j == 16)
						{
							stock.isLimit = data[j];
						}
						else if(j == 17)
						{
							stock.marginUnit = data[j];
						}
						else if(j == 18)
						{
							stock.roundLot = data[j];
						}
						else if(j == 19)
						{
							stock.inVal = data[j];
						}
						else if(j == 20)
						{
							stock.timeVal = data[j];
						}
						else if(j == 21)
						{
							stock.preInterest = data[j];
						}
						else if(j == 22)
						{
							stock.openInterest = data[j];
						}
						else if(j == 23)
						{
							stock.tradePhase = data[j];
						}
						else if(j == 24)
						{
							stock.remainDate = data[j];
						}
						else if(j == 25)
						{
							stock.leverage = data[j];
						}
						else if(j == 26)
						{
							stock.premium = data[j];
						}
					}
				}
			}
		}
		
		return stock;
	}

	public static Stock[] parse(String src)
	{
		Stock[] list = null;
		String[] rows = src.split(ParseUtil.SPLIT_0x03);

		if(rows.length > 0)
		{
			list = new Stock[rows.length];

			for(int i = 0; i < rows.length; i++)
			{
				String[] column = rows[i].split(ParseUtil.SPLIT_0x02);

				Stock stock = new Stock();

				for(int j = 0; j < column.length; j++)
				{
					parseColumn(j, column[j], stock);
				}

				calculateColumnValue(stock);

				/**
				 * 格式化欄位
				 */
				formatColumnValue(stock);
				list[i] = stock;
			}
		}

		return list;
	}

	private static void parseColumn(int i, String column, Stock stock)
	{
		if(i == 0) // 股票状态
		{
			stock.status = column;
		}
		else if(i == 1) // 代码
		{
			stock.id = column;
		}
		else if(i == 2) // 名称
		{
			stock.name = column;
		}
		else if(i == 3) // 交易时间
		{
			stock.datetime = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 4) // 拼音
		{
			stock.pinyin = column;
		}
		else if(i == 5) // 市场别
		{
			stock.market = column;
		}
		else if(i == 6) // 次类别
		{
			stock.subtype = column;
		}
		else if(i == 7) // 最新价
		{
			stock.lastPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 8) // 最高价
		{
			stock.highPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 9) // 最低价
		{
			stock.lowPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 10) // 今开价
		{
			stock.openPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 11) // 昨收价
		{
			stock.preClosePrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 12) // 涨跌比率
		{
			stock.changeRate = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 13) // 总量
		{
			stock.volume = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 14) // 当前成交量
		{
			stock.nowVolume = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 15) // 换手率
		{
			stock.turnoverRate = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 16) // 涨停价
		{
			stock.limitUP = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 17) // 跌停价
		{
			stock.limitDown = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 18) // 均价
		{
			stock.averageValue = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 19) // 涨跌
		{
			/**
			 * 目前這個欄位沒有值,要自已算
			 */
			stock.change = column;
		}
		else if(i == 20) // 成交金额
		{
			stock.amount = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 21) // 量比
		{
			stock.volumeRatio = column;
		}
		else if(i == 22) // 买一价
		{
			stock.buyPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 23) // 卖一价
		{
			stock.sellPrice = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 24) // 外盘量
		{
			stock.buyVolume = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 25) // 内盘量
		{
			stock.sellVolume = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 26) // 总市值
		{
			stock.totalValue = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 27) // 流值
		{
			stock.flowValue = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 28) // 净资产
		{
			stock.netAsset = column;
		}
		else if(i == 29) // PE(市盈)
		{
			stock.pe = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 30) // ROE(净资产收益率)
		{
			stock.roe = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 31) // 总股本
		{
			stock.capitalization = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 32) // 流通股
		{
			stock.circulatingShares = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 33) // 五档买价
		{
			if(column != null && column.length() > 0)
			{
				stock.buyPrices = new ArrayList<String>();
				String[] temp = column.split(",");

				for(int j = 0; j < temp.length; j++)
				{
					temp[j] = ParseUtil.base93.getDecodeNumber(temp[j].trim());
				}

				Collections.addAll(stock.buyPrices, temp);
			}
		}
		else if(i == 34) // 五档买量
		{
			if(column != null && column.length() > 0)
			{
				stock.buyVolumes = new ArrayList<String>();
				stock.buySingleVolumes = new ArrayList<String>();

				String[] temp = column.split(",");
				String[] volume = new String[temp.length];
				String[] single = new String[temp.length];

				for(int j = 0; j < temp.length; j++)
				{
					String[] v = temp[j].trim().split("[|]", 2);

					volume[j] = ParseUtil.base93.getDecodeNumber(v[0]);
					single[j] = ParseUtil.base93.getDecodeNumber(v[1]);

					stock.buyVolumesTmp += volume[j] + "_";
					stock.buySingleVolumesTmp += single[j] + "_";
				}

				Collections.addAll(stock.buyVolumes, volume);
				Collections.addAll(stock.buySingleVolumes, single);
			}
		}
		else if(i == 35) // 五档卖价
		{
			if(column != null && column.length() > 0)
			{
				stock.sellPrices = new ArrayList<String>();
				String[] temp = column.split(",");

				for(int j = 0; j < temp.length; j++)
				{
					temp[j] = ParseUtil.base93.getDecodeNumber(temp[j].trim());
				}

				Collections.addAll(stock.sellPrices, temp);
			}
		}
		else if(i == 36) // 五档卖量
		{
			if(column != null && column.length() > 0)
			{
				stock.sellVolumes = new ArrayList<String>();
				stock.sellSingleVolumes = new ArrayList<String>();

				String[] temp = column.split(",");
				String[] volume = new String[temp.length];
				String[] single = new String[temp.length];

				for(int j = 0; j < temp.length; j++)
				{
					String[] v = temp[j].trim().split("[|]", 2);

					volume[j] = ParseUtil.base93.getDecodeNumber(v[0]);
					single[j] = ParseUtil.base93.getDecodeNumber(v[1]);

					stock.sellVolumesTmp += volume[j] + "_";
					stock.sellSingleVolumesTmp += single[j] + "_";
				}

				Collections.addAll(stock.sellVolumes, volume);
				Collections.addAll(stock.sellSingleVolumes, single);
			}
		}
		else if(i == 37) // 振幅比率
		{
			stock.amplitudeRate = ParseUtil.base93.getDecodeNumber(column);
		}
		else if(i == 38) // 收益
		{
			stock.receipts = column.trim();
		}
	}

	private static void calculateColumnValue(Stock stock)
	{
		if(isZero(stock.lastPrice))
		{
			stock.lastPrice = null;
			stock.change = null;
		}
		else
		{
			if(isZero(stock.preClosePrice))
			{
				stock.preClosePrice = null;
			}
			else
			{
				long change = Long.parseLong(stock.lastPrice) - Long.parseLong(stock.preClosePrice);

				if(change == 0)
				{
					stock.change = null;
				}
				else
				{
					stock.change = Long.toString(change);

					if(change > 0)
					{
						stock.change = "+" + stock.change;
					}
				}
			}
			/**
			 * 漲停
			 */
			if(isZero(stock.limitUP))
			{
				stock.limitUP = null;
			}
			/**
			 * 跌停
			 */
			if(isZero(stock.limitDown))
			{
				stock.limitDown = null;
			}
		}

		/**
		 * 計算涨跌幅
		 */
		if(isZero(stock.change) || isZero(stock.preClosePrice))
		{
			stock.changeRate = null;
		}
		else
		{
			double changeRate = Double.parseDouble(stock.change) / Double.parseDouble(stock.preClosePrice) * 100;
			changeRate = changeRate < 0 ? changeRate * -1 : changeRate;
			stock.changeRate = formatSuffixLen(Double.toString(changeRate), 2);
		}

		/**
		 * 计算换手率,总手/流通股数(%)
		 */
		if(isZero(stock.volume) || isZero(stock.circulatingShares))
		{
			stock.turnoverRate = null;
		}
		else
		{
			double turnoverRate = Double.parseDouble(stock.volume) / Double.parseDouble(stock.circulatingShares) * 100;
			stock.turnoverRate = formatSuffixLen(Double.toString(turnoverRate), 2);
		}

		/**
		 * 计算量比: 总手与过去5个交易日每分钟平均成交量之比 v/ref(ma(v,5),1) 不知道要怎麼算
		 */
		/**
		 * 计算市盈: 现价/折算成全年的每股收益
		 */
		if(isZero(stock.lastPrice) || isZero(stock.receipts))
		{
			stock.pe = null;
		}
		else
		{

			double pe = Double.parseDouble(stock.lastPrice) / Double.parseDouble(stock.receipts) / offer(stock.subtype);
			stock.pe = formatSuffixLen(Double.toString(pe), 2);
		}

		/**
		 * 计算市净率: 现价/每股净资产
		 */
		if(isZero(stock.lastPrice) || isZero(stock.netAsset))
		{
			stock.roe = null;
		}
		else
		{
			double roe = Double.parseDouble(stock.lastPrice) / Double.parseDouble(stock.netAsset) / offer(stock.subtype);
			stock.roe = formatSuffixLen(Double.toString(roe), 2);
		}

		/**
		 * 计算总市值: 总股本*现价
		 */
		if(isZero(stock.lastPrice) || isZero(stock.capitalization))
		{
			stock.totalValue = null;
		}
		else
		{
			long totalValue = Long.parseLong(stock.lastPrice) * Long.parseLong(stock.capitalization) / offer(stock.subtype);
			stock.totalValue = Long.toString(totalValue);
		}

		/**
		 * 计算流值: 流通股本*现价
		 */
		if(isZero(stock.lastPrice) || isZero(stock.circulatingShares))
		{
			stock.flowValue = null;
		}
		else
		{
			long flowValue = Long.parseLong(stock.lastPrice) * Long.parseLong(stock.circulatingShares) / offer(stock.subtype);
			stock.flowValue = Long.toString(flowValue);
		}

		/**
		 * [tse] 振幅
		 */

		if(true == isZero(stock.preClosePrice) || stock.highPrice == null || stock.lowPrice == null || stock.preClosePrice == null
				|| stock.highPrice.equals("-") || stock.lowPrice.equals("-") || stock.preClosePrice.equals("-"))
		{
			stock.amplitudeRate = null;
		}
		else
		{
			float flHigh = Float.parseFloat(stock.highPrice);
			float flLow = Float.parseFloat(stock.lowPrice);
			float flPreClose = Float.parseFloat(stock.preClosePrice);

			if((flHigh - flLow) != 0)
			{
				double value = Math.abs(flHigh - flLow) / flPreClose * 100;
				stock.amplitudeRate = formatSuffixLen(Double.toString(value), 2);
			}
			else
			{
				stock.amplitudeRate = null;
			}
		}

		/**
		 * 计算委比 委比的计算公式为：委比=(委买手数－委卖手数)/(委买手数+委卖手数)×100%
		 *
		 */
		if(stock.buyVolumes == null && stock.sellVolumes == null)
		{
			stock.orderRatio = null;
		}
		else
		{
			double buyTotal = 0;
			double sellTotal = 0;

			if(stock.buyVolumes != null)
			{
				for(int i = 0; i < stock.buyVolumes.size(); i++)
				{
					buyTotal += Double.parseDouble(stock.buyVolumes.get(i));
				}
			}
			else
			{
				buyTotal = 0;
			}

			if(stock.sellVolumes != null)
			{
				for(int i = 0; i < stock.sellVolumes.size(); i++)
				{
					sellTotal += Double.parseDouble(stock.sellVolumes.get(i));
				}
			}
			else
			{
				sellTotal = 0;
			}

			stock.orderRatio = fourOutButFiveInWithPoint(Double.toString(((buyTotal - sellTotal) / (buyTotal + sellTotal)) * 100), 2) + "%";

		}
	}

	private static int offer(String subType)
	{
		if(subType.equals("1400")) // 指数
		{
			return 100000;
		}
		else
		{
			return 1000;
		}
	}

	private static boolean isZero(String src)
	{
		if(src == null || src.length() == 0)
		{
			return true;
		}
		else
		{
			String check = src;

			if(src.indexOf("-") == 0 || src.indexOf("+") == 0)
			{
				check = src.substring(1);
			}

			if(check.length() > 0 && Float.parseFloat(check) == 0)
			{
				return true;
			}
		}

		return false;
	}

	private static String formatSuffixLen(String src, int len)
	{
		if(src != null && src.length() > 0 && len > 0)
		{
			StringBuilder sb = new StringBuilder(src);
			int dot = sb.indexOf(".");

			if(dot == -1) // 沒有小數位數,就加上去
			{
				sb.append(".");

				for(int i = 0; i < len; i++)
				{
					sb.append("0");
				}

			}
			else
			{
				int suffixLen = sb.length() - dot - 1;

				if(suffixLen > len)
				{
					sb = fourOutButFiveInWithPoint(sb.toString(), len);
				}
				else
				{
					for(int i = 0, end = (len - suffixLen); i < end; i++)
					{
						sb.append("0");
					}
				}
			}
			return sb.toString();
		}
		else
		{
			return src;
		}
	}

	private static StringBuilder fourOutButFiveInWithPoint(String str, int suffixRetainLen)
	{
		str = checkECalculate(Float.parseFloat(str));

		if(str.contains("."))
		{
			float f = Float.parseFloat(str);
			String mStr = format(f, suffixRetainLen);// 格式化成String型并保留小数点后位数

			StringBuilder finalStr = new StringBuilder(mStr);
			return finalStr;
		}

		return new StringBuilder(str);
	}

	private static String checkECalculate(float f)
	{
		String str = f + "";

		if(str.contains("E"))
		{
			return format(f, 6);
		}
		return str;
	}

	private static String format(float fpre, int suffixRetainLen)
	{
		String src;

		switch (suffixRetainLen)
		{
			case 1:
				src = String.format("%.1f", fpre);
				break;
			case 2:
				src = String.format("%.2f", fpre);
				break;
			case 3:
				src = String.format("%.3f", fpre);
				break;
			case 4:
				src = String.format("%.4f", fpre);
				break;
			case 5:
				src = String.format("%.5f", fpre);
				break;
			case 6:
				src = String.format("%.6f", fpre);
				break;
			default:
				src = String.format("%.2f", fpre);

		}
		return src;
	}

	private static void formatColumnValue(Stock stock)
	{
		stock.change = formatPrice(stock.change, stock.market, stock.subtype); // 漲跌
		stock.lastPrice = formatPrice(stock.lastPrice, stock.market, stock.subtype); // 最新价
		stock.highPrice = formatPrice(stock.highPrice, stock.market, stock.subtype); // 最高价
		stock.lowPrice = formatPrice(stock.lowPrice, stock.market, stock.subtype); // 最低价
		stock.openPrice = formatPrice(stock.openPrice, stock.market, stock.subtype); // 今开价
		stock.preClosePrice = formatPrice(stock.preClosePrice, stock.market, stock.subtype); // 昨收价
		stock.limitUP = formatPrice(stock.limitUP, stock.market, stock.subtype); // 涨停价
		stock.limitDown = formatPrice(stock.limitDown, stock.market, stock.subtype); // 跌停价
		stock.averageValue = formatPrice(stock.averageValue, stock.market, stock.subtype); // 均价
		stock.buyPrice = formatPrice(stock.buyPrice, stock.market, stock.subtype); // 买一价
		stock.sellPrice = formatPrice(stock.sellPrice, stock.market, stock.subtype); // 卖一价

		if(stock.buyPrices != null && stock.buyPrices.size() > 0)
		{
			for(int i = 0; i < stock.buyPrices.size(); i++) // 五档买价
			{
				stock.buyPrices.set(i, formatPrice(stock.buyPrices.get(i), stock.market, stock.subtype));
				stock.buyPricesTmp += stock.buyPrices.get(i) + "_";
			}
		}

		if(stock.sellPrices != null && stock.sellPrices.size() > 0)
		{
			for(int i = 0; i < stock.sellPrices.size(); i++) // 五档卖价
			{
				stock.sellPrices.set(i, formatPrice(stock.sellPrices.get(i), stock.market, stock.subtype));
				stock.sellPricesTmp += stock.buyPrices.get(i) + "_";
			}
		}
		/**
		 * 净资产
		 */
		stock.netAsset = formatSuffixLen(stock.netAsset, 2);
	}

	private static String formatPrice(String src, String market, String subType)
	{
		if(src != null && !isZero(src) && market != null && subType != null)
		{
			int driftLen = 0;
			int suffixRetainLen = 0;

			if(market.equals("sh")) // 上证
			{
				if(subType.equals("1002")) // 上证B股
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
				else if(subType.equals("1100")) // 上证基金
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
				else if(subType.equals("1300")) // 上证债券
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
				else if(subType.equals("1400")) // 大盘指数
				{
					driftLen = 5;
					suffixRetainLen = 2;
				}
				else if(subType.equals("3002")) // 上证期权
				{
					driftLen = 4;
					suffixRetainLen = 4;
				}
				else
				// 全市场
				{
					driftLen = 3;
					suffixRetainLen = 2;
				}
			}
			else if(market.equals("sz")) // 深证
			{
				if(subType.equals("1100")) // 深证基金
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
				else if(subType.equals("1300")) // 深证债券
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
				else if(subType.equals("1400")) // 大盘指数
				{
					driftLen = 3;
					suffixRetainLen = 2;
				}
				else
				// 全市场
				{
					driftLen = 3;
					suffixRetainLen = 2;
				}
			}
			else if(market.equals("hk")) // 港股
			{
				if(subType.equals("1400")) // 大盘指数
				{
					driftLen = 4;
					suffixRetainLen = 3;
				}
				else
				// 全市场
				{
					driftLen = 3;
					suffixRetainLen = 3;
				}
			}

			return formatPrice(src, driftLen, suffixRetainLen);
		}

		return "-";
	}

	private static String formatPrice(String src, int driftLen, int suffixRetainLen)
	{
		StringBuffer sb = new StringBuffer(src);

		String mark = null;

		if(sb.indexOf("-") == 0 || sb.indexOf("+") == 0)
		{
			mark = sb.substring(0, 1);
			sb.deleteCharAt(0);
		}

		int suffixLen = 0;
		/**
		 * 先看總長度夠不夠
		 */
		if(sb.length() > driftLen)
		{
			int dot = sb.length() - driftLen;// 计算超长的位数

			sb.insert(dot, ".");

			suffixLen = sb.length() - (dot + 1) - suffixRetainLen;
		}
		else if(sb.length() < driftLen)
		{
			/**
			 * 總長度小於小數點位數,就在前面補0
			 */
			while(sb.length() < driftLen)
			{
				sb.insert(0, "0");
			}

			sb.insert(0, "0.");

			suffixLen = sb.length() - 2 - suffixRetainLen;
		}
		else
		{
			/**
			 * 總長度相等小數點位數就直接加入0.
			 */
			sb.insert(0, "0.");

			suffixLen = sb.length() - 2 - suffixRetainLen;
		}

		/**
		 * 去掉多餘尾數
		 */
		if(suffixLen > 0)
		{
			sb = new StringBuffer(fourOutButFiveInWithPoint(sb.toString(), suffixRetainLen).toString());
		}
		else if(suffixLen < 0)
		{
			suffixLen *= -1;

			while(suffixLen < suffixRetainLen)
			{
				sb.insert(sb.length(), "0");
				suffixLen++;
			}
		}

		if(mark != null)
		{
			sb.insert(0, mark);
		}

		return sb.toString();
	}
}
